package com.example.controller;
import com.example.pojo.Dept;
import com.example.pojo.Result;
import com.example.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
//    private static Logger log = LoggerFactory.getLogger(DeptController.class);
@Autowired
private DeptService deptService;
//    @RequestMapping(value = "/depts" ,method = RequestMethod.GET)  //指定请求方式为get
    @GetMapping
    public Result list()
    {
         log.info("查询全部部门");
         //调用service查询
        List<Dept> deptlist = deptService.list();
         return Result.success(deptlist);
    }
    //根据id删除数据
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) throws Exception {
       log.info("根据id删除部门{}",id);
       deptService.delete(id);
       return Result.success();
    }

    @PostMapping
    public Result addDept(@RequestBody Dept dept)
    {
        log.info("新增部门{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }
    //根据id查询数据
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id)
    {
        log.info("查询部门{}" ,id);
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }
    //更新部门
    @PutMapping
    public Result upadte(@RequestBody Dept dept)
    {
        log.info("更新部门{}",dept);
        deptService.update(dept);
        return Result.success();
    }

}
